# -*- coding: utf-8 -*-


#对图像进行模板匹配与图像和模板之间的互相关
import numpy as np
from scipy import ndimage
from scipy import misc
from matplotlib import pylab
from scipy import signal
face_image=misc.face(gray=True)-misc.face(gray=True).mean()
template_image=np.copy(face_image[300:365,670:750])#切片右眼的数据,用来做模板
template_image-=template_image.mean()
pylab.imshow(template_image,cmap='gray')
face_image=face_image+np.random.randn(*face_image.shape)*50#随机添加噪音
correlation=signal.correlate2d(face_image,template_image,boundary='symm',mode='same')
y,x=np.unravel_index(np.argmax(correlation),correlation.shape)
#显示图像
pylab.figure(figsize=(6,15))
pylab.subplot(311)
pylab.imshow(face_image,cmap='gray')
pylab.title('Original',size=20)
pylab.axis('off')

pylab.subplot(312)
pylab.imshow(template_image,cmap='gray')
pylab.title('Template',size=20)
pylab.axis('off')

pylab.subplot(313)
pylab.imshow(correlation,cmap='afmhot')
pylab.title('Cross-correlation',size=20)
pylab.axis('off')

pylab.plot(x,y,'ro')#模板匹配坐标
pylab.show()





